package com.example.demo.lock;

public class SynTest2 implements Runnable{

    public static  int i = 0;


    public void add(){
        synchronized (SynTest2.class){
            for (int j = 0;j<100000;j++){
                i++;
            }
        }
    }

    @Override
    public void run() {
        add();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new SynTest2());
        Thread t2 = new Thread(new SynTest2());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(i);
    }
}

class TestSyn{

    public static void main(String[] args) throws InterruptedException {
       Thread t1 = new Thread(new SynTest2());
       Thread t2 = new Thread(new SynTest2());
       t1.start();
       t2.start();
       t1.join();
       t2.join();
       System.out.println(SynTest2.i);

    }
}